package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.mapper.ne.NeOverviewItemMapper;
import com.nari.ncps.screen.service.ne.NeOverviewItemService;
import com.nari.ncps.screen.vo.ne.NeOverviewIndicatorsVO;
import com.nari.ncps.screen.entity.ne.NeOverviewItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 新能源产业主要指标表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOverviewItemServiceImpl extends ServiceImpl<NeOverviewItemMapper, NeOverviewItem> implements NeOverviewItemService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public NeOverviewIndicatorsVO getMainIndicators(Integer year) {
        // 如果没有指定年份，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }

        int windFarmStationNum = 0;//风电场数量
        int pvPowerStationnum = 0;//光伏场数量
        BigDecimal windInstalledCapacity = new BigDecimal(0);//集中式风电装机容量（万千瓦）
        BigDecimal distriwindInstalledCapacity = new BigDecimal(0);//分布式风电装机容量（万千瓦）
        BigDecimal windUnderConstruction = new BigDecimal(0);//风电在建容量（万千瓦）
        BigDecimal centralizedPvCapacity = new BigDecimal(0);//集中式光伏容量（万千瓦）
        BigDecimal distributedPvCapacity = new BigDecimal(0);//分布式光伏容量（万千瓦）
        BigDecimal pvUnderConstruction = new BigDecimal(0);//光伏在建容量（万千瓦）
        String sql1 = "SELECT station_type, MAX(capacity) as max_capacity FROM qt_dp_ne_overview_item " +
                "WHERE org_code = '100360' AND year = ?  " +
                " AND station_type is not NULL "+
                "GROUP BY station_type " ;
//        try {
//            List<Map<String, Object>> resultMap = jdbcTemplate.queryForList(sql1, year);
//
//            for (Map<String, Object> row : resultMap) {
//                String stationType = (String) row.get("station_type");
//                BigDecimal maxCapacity = row.get("capacity") != null ?
//                        new BigDecimal(row.get("capacity").toString()) : BigDecimal.ZERO;
//                //场站类型:01全部、02集中式新能源、03集中式风电、04集中式光伏、05三新产业、06分布式风电、07分布式光伏、00 空
//                // 根据 station_type 处理不同的容量
//                if ("03".equals(stationType)) {
//                    windInstalledCapacity = (BigDecimal) row.get("capacity");
//                } else if ("04".equals(stationType)) {
//                    centralizedPvCapacity = (BigDecimal) row.get("capacity");
//                }else if ("07".equals(stationType)) {
//                    distributedPvCapacity = (BigDecimal) row.get("capacity");
//                }
//            }
//        } catch (EmptyResultDataAccessException e) {
//            e.printStackTrace();
//        }

        try {
            List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql1, year);

            for (Map<String, Object> row : resultList) {
                String stationType = (String) row.get("station_type");
                BigDecimal capacity = row.get("max_capacity") != null ? new BigDecimal(row.get("max_capacity").toString()) : BigDecimal.ZERO;

                // 根据 station_type 赋值
                //场站类型:01全部、02集中式新能源、03集中式风电、04集中式光伏、05三新产业、06分布式风电、07分布式光伏、00 空
                switch (stationType) {
                    case "03":
                        windInstalledCapacity = capacity;
                        break;
                    case "04":
                        centralizedPvCapacity = capacity;
                        break;
                    case "06":
                        distriwindInstalledCapacity = capacity;
                        break;
                    case "07":
                        distributedPvCapacity = capacity;
                        break;
                    default:
//                        log.debug("未知的电站类型: {}", stationType);
                        break;
                }
            }

        } catch (Exception e) {
            log.error("查询新能源容量失败", e);
        }

        String sql2 = "SELECT wind_farm_station_num,pv_power_station_num  FROM xny_org " +
                "WHERE org_code = '100360' limit  1 " ;
        try {
            Map<String, Object> resultMap = jdbcTemplate.queryForMap(sql2);
            windFarmStationNum = (int) resultMap.get("wind_farm_station_num");
            pvPowerStationnum = (int) resultMap.get("pv_power_station_num");

        } catch (EmptyResultDataAccessException  e) {
            e.printStackTrace();
        }

        // 构建返回VO
        NeOverviewIndicatorsVO vo = new NeOverviewIndicatorsVO();
        vo.setWindFarmCount(windFarmStationNum);
        vo.setWindInstalledCapacity(windInstalledCapacity);
        vo.setWindUnderConstruction(windUnderConstruction);
        vo.setDistriwindInstalledCapacity(distriwindInstalledCapacity);
        vo.setPvStationCount(pvPowerStationnum);
        vo.setCentralizedPvCapacity(centralizedPvCapacity);
        vo.setDistributedPvCapacity(distributedPvCapacity);
        vo.setPvUnderConstruction(pvUnderConstruction);

        return vo;
    }

}
